﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABELGX
{
    public class Sensor : INotifyPropertyChanged
    {
        private double sourceValue = 1E-10;
        private double value;
        public SensorSet Set {  get; }
        public double Value 
        {
            get => value;
            set
            {
                if (sourceValue == value)
                {
                    return;
                }
                sourceValue = value;
                this.value = sourceValue * Set.Scale + Set.Offset;
                ValueText = value.ToString(Set.Format);
                ValueUnit = $"{this.value.ToString(Set.Format)}{Set.Unit}";
                ValueString = $"{Set.Name}：{this.value.ToString(Set.Format)} {Set.Unit}";
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ValueText"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ValueUnit"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ValueString"));
            }
        }
        public string ValueString { get; private set; }
        public string ValueText {  get; private set; }
        public string ValueUnit { get; private set; }
        public List<SensorLevel> Levels { get; set; } = new List<SensorLevel>();

        private int status;
        public int Status 
        { get => status;
            set
            {
                if (status == value)
                {
                    return;
                }
                status = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Status"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Level"));
            }
        }
        public SensorLevel Level => Levels.Find(x => x.Level == Status);
        public Sensor(SensorSet set)
        {
            Set = set;
            Levels.Add(new SensorLevel(0, "正常", "Lime"));
            Levels.Add(new SensorLevel(1, "故障", "Red"));
            Levels.Add(new SensorLevel(2, "一级告警", "Yellow"));
            Levels.Add(new SensorLevel(3, "二级告警", "Orange"));
            //Levels.Add(new SensorLevel(0, "正常", Color.Lime));
            //Levels.Add(new SensorLevel(1, "故障", Color.Red));
            //Levels.Add(new SensorLevel(2, "一级告警", Color.Yellow));
            //Levels.Add(new SensorLevel(3, "二级告警", Color.Orange));
            if (set.Levels != null)
            {
                foreach (var level in set.Levels)
                {
                    var find = Levels.Find(x => x.Level == level.Level);
                    if (find != null)
                    {
                        find.Color = level.Color;
                        find.Text = level.Text;
                    }
                }
            }
        }
        public Sensor(SensorSet set, List<SensorLevel> levels) : this(set)
        {
            Levels = levels;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        //public void SetInfo(params string[] infos)
        //{
        //    StateInfo.Clear();
        //    StateInfo.AddRange(infos);
        //}
        //public void SetColor(params Color[] colors)
        //{
        //    StateColor.Clear();
        //    StateColor.AddRange(colors);
        //}
        //public override string ToString()
        //{
        //    return $"{Value.ToString(Format)} {Unit}";
        //}
        //public string GetLabelData()
        //{
        //    return $"{Name}：{Value.ToString(Format)} {Unit}";
        //}
        //public void SaveWarningLog()
        //{
        //    if(State>0)
        //    {
        //        Config.WriteWarning($"{Name},{StateInfo[State]}");
        //    }
        //}
    }
}
